from flask import Flask,request, jsonify
import pandas as pd
from itertools import combinations

app = Flask(__name__)


frequest_itensets = pd.read_pickle('./frequent_itemsets.pkl')
rules = pd.read_pickle('./rule.pkl')

@app.route("/recommend", methods=['POST'])
def recommend():
    data = request.json.get('items', [])


    recommendDations = []
    for idx, rule in rules.iterrows():
        antecedants = list(rule['antecedents'])
        consequents = list(rule['consequents'])
        
        
        if set(antecedants).issubset(set(data)):
            recommendDations.extend(consequents)
            
    recommendDations = list(set(recommendDations) - set(data))
    return jsonify ({'recommendDations': recommendDations})

if __name__ == "__main__":
    app.run()